package com.csun.cmny.provider.controller;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.constant.MedicConstant;
import com.csun.cmny.provider.model.domain.OmcNursingUserBind;
import com.csun.cmny.provider.model.dto.medic.MedicRecordConfirmDto;
import com.csun.cmny.provider.model.dto.medic.MedicRecordFeedDto;
import com.csun.cmny.provider.model.dto.medic.MedicRecordQueryDto;
import com.csun.cmny.provider.model.dto.medic.MedicRecordTimeOutDto;
import com.csun.cmny.provider.model.vo.MedicRecordVo;
import com.csun.cmny.provider.pojo.NursingGroup;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcGroupService;
import com.csun.cmny.provider.service.OmcMedicRecordService;
import com.csun.cmny.provider.service.OmcNursingUserBindService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-08-13 15:12
 * @Description: the class omc medic record controller
 */
@RestController
@RequestMapping(value = "/medicRecord", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcMedicRecordController extends BaseController {

    @Resource
    private OmcGroupService omcGroupService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcMedicRecordService omcMedicRecordService;
    @Resource
    private OmcNursingUserBindService omcNursingUserBindService;

    /**
     * 查询用药记录列表
     *
     * @return
     */
    @PostMapping(value = "/queryListWithPage")
    public Wrapper queryListWithPage(@RequestBody MedicRecordQueryDto medicRecordQueryDto) {

        medicRecordQueryDto.setGroupId(getLoginAuthDto().getGroupId());

        List<MedicRecordVo> medicRecordVoList = omcMedicRecordService.queryListWithPage(medicRecordQueryDto);

//        return WrapMapper.ok(new PageInfo<>(medicRecordVoList));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), new PageInfo<>(medicRecordVoList));
    }

    /**
     * 查询任务列表
     *
     * @return
     */
    @PostMapping(value = "/queryCurTask")
    public Wrapper queryCurTask(@RequestBody MedicRecordQueryDto medicRecordQueryDto) {

        Long userId = getLoginAuthDto().getUserId();
        List<Integer> states = Lists.newArrayList();

        // 根据用户查询所在班组
        OmcNursingUserBind omcNursingUserBind = new OmcNursingUserBind();
        omcNursingUserBind.setUserId(userId);
        omcNursingUserBind = omcNursingUserBindService.selectOne(omcNursingUserBind);
        if (omcNursingUserBind == null) {
            throw new OmcBizException(ErrorCodeEnum.OMC10038008);
        }

        // 判断是否为班长
        NursingGroup nursingGroup = new NursingGroup();
        nursingGroup.setUserId(userId);
        nursingGroup = omcGroupService.selectOne(nursingGroup);

        states.add(MedicConstant.MEDIC_RECORD_STATE_APPLY);
        states.add(MedicConstant.MEDIC_RECORD_STATE_FEED);
        if (nursingGroup != null) {
            // 班长
            states.add(MedicConstant.MEDIC_RECORD_STATE_NURSING_CONFIRM);
            states.add(MedicConstant.MEDIC_RECORD_STATE_DELAY_FEED);
            states.add(MedicConstant.MEDIC_RECORD_STATE_DELAY_NURSING_CONFIRM);
            states.add(MedicConstant.MEDIC_RECORD_STATE_DELAY_MONITOR_CONFIRM);
        }

        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String d = sdf.format(date);
        medicRecordQueryDto.setNow(d);
        medicRecordQueryDto.setStates(states);
        medicRecordQueryDto.setNursingGroupId(omcNursingUserBind.getNursingGroupId());
        List<MedicRecordVo> medicRecordVoList = omcMedicRecordService.queryListWithPage(medicRecordQueryDto);

//        return WrapMapper.ok(new PageInfo<>(medicRecordVoList));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), new PageInfo<>(medicRecordVoList));
    }

    /**
     * 查询历史任务
     *
     * @return
     */
    @PostMapping(value = "/queryHisTask")
    public Wrapper queryHisTask(@RequestBody MedicRecordQueryDto medicRecordQueryDto) {

        // 根据用户查询所在班组
        Long userId = getLoginAuthDto().getUserId();
        List<Integer> states = Lists.newArrayList();

        // 根据用户查询所在班组
        OmcNursingUserBind omcNursingUserBind = new OmcNursingUserBind();
        omcNursingUserBind.setUserId(userId);
        omcNursingUserBind = omcNursingUserBindService.selectOne(omcNursingUserBind);
        if (omcNursingUserBind == null) {
            throw new OmcBizException(ErrorCodeEnum.OMC10038008);
        }

        states.add(MedicConstant.MEDIC_RECORD_STATE_MONITOR_CONFIRM);
        states.add(MedicConstant.MEDIC_RECORD_STATE_TIME_OUT);

        medicRecordQueryDto.setStates(states);
        medicRecordQueryDto.setNursingGroupId(omcNursingUserBind.getNursingGroupId());

        List<MedicRecordVo> medicRecordVoList = omcMedicRecordService.queryListWithPage(medicRecordQueryDto);

//        return WrapMapper.ok(new PageInfo<>(medicRecordVoList));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), new PageInfo<>(medicRecordVoList));
    }

    @PostMapping(value = "/apply")
    public Wrapper apply(@RequestBody Map<String, Object> param) {

        Integer result = omcMedicRecordService.apply(param, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 护工喂药
     *
     * @return
     */
    @ValidateAnnotation
    @PostMapping(value = "/feed")
    public Wrapper feed(@Validated @RequestBody MedicRecordFeedDto medicRecordFeedDto,
                        BindingResult br) {

        Integer result = omcMedicRecordService.feed(medicRecordFeedDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);

    }

    /**
     * 喂药确认
     *
     * @return
     */
    @ValidateAnnotation
    @PostMapping(value = "/nursingConfirm")
    public Wrapper nursingConfirm(@Validated @RequestBody MedicRecordConfirmDto medicRecordConfirmDto,
                                  BindingResult br) {

        Integer result = omcMedicRecordService.nursingConfirm(medicRecordConfirmDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 班长确认
     *
     * @return
     */
    @ValidateAnnotation
    @PostMapping(value = "/monitorConfirm")
    public Wrapper monitorConfirm(@Validated @RequestBody MedicRecordConfirmDto medicRecordConfirmDto,
                                  BindingResult br) {

        Integer result = omcMedicRecordService.monitorConfirm(medicRecordConfirmDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 超时处理
     *
     * @return
     */
    @ValidateAnnotation
    @PostMapping(value = "/timeOut")
    public Wrapper timeOut(@Validated @RequestBody MedicRecordTimeOutDto medicRecordTimeOutDto, BindingResult br) {

        Integer result = omcMedicRecordService.timeOut(medicRecordTimeOutDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }
}
